package com.netty.modbus.dto.request;

import com.netty.modbus.exception.DataConvertException;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

/**
 * description: ReadBody <br>
 * date: 2021/10/8 16:45 <br>
 * @author: lwy <br>
 * version: 1.0 <br>
 */
public class ReadBody extends RequestBody{
    /**
     * 读的功能码
     * */
    private final byte[] functionCode={1,3};

    /**
     * modbus数据内容
     * */
    private final byte[] data;

    /**
     * 读取寄存器数量
     * */
    private final byte[] quantity;
    /**
     * 读取寄存器起始地址
     * */
    private final byte[] startAddress;

    private String devNumber;

    public String getDevNumber() {
        return devNumber;
    }

    public void setDevNumber(String devNumber) {
        this.devNumber = devNumber;
    }

    public ReadBody(byte[] startAddress, byte[] quantity) {
       ByteBuf dataBuf= Unpooled.buffer();
       dataBuf.writeBytes(functionCode);
       dataBuf.writeBytes(startAddress);
       dataBuf.writeBytes(quantity);
       data=new byte[dataBuf.readableBytes()];
       this.quantity=quantity;
       this.startAddress=startAddress;
       dataBuf.readBytes(data);
    }

    /**
     * description: 编码
     * version: 1.0
     * date: 2021/10/9 9:58
     * @author: lwy
     * @param
     * @return io.netty.buffer.ByteBuf
     */
    @Override
    public ByteBuf encode() throws DataConvertException {
        return this.commEncode(this.data);
    }

    public byte[] getQuantity() {
        return quantity;
    }

    public byte[] getStartAddress() {
        return startAddress;
    }
}
